package com.szxy.service.impl;

import com.szxy.service.BusinessService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zwz
 * @Date:2020/7/15 22:37
 * @Description:com.szxy.service
 * @Version:1.0
 **/
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private OrderServiceApi orderServiceApi;
    @Autowired
    private StorageServiceApi storageServiceApi;

    /**
     * 记录日志
     */
    private static final Logger logger = LoggerFactory.getLogger(BusinessService.class);



    @Override
    public void purcase(String userId, String productId, int count) {
        logger.info("开始购买商品，用户{}，商品号{}，商品数量{}", userId, productId, count);
        //1. 扣库存
        storageServiceApi.dePuct(productId, count);
        //2. 下订单
        orderServiceApi.create(userId, productId, count);
        logger.info("购买商品结束，用户{} ...", userId);
        String xid = RootContext.getXID();
        System.out.println("全局事务 xid = " + xid);
    }
}
